
import uuid
from datetime import datetime
import uuid
from datetime import datetime
class Database:
    def __init__(self):
        self.fee_records = []  # 费用记录表
        self.payment_records = []  # 支付记录表
        self.repair_requests = []  # 报修记录表
    def insert_fee_record(self, record):
        self.fee_records.append(record)

    # 插入支付记录
    def insert_payment_record(self, record):
        self.payment_records.append(record)

    # 查询费用记录
    def query_fee_records(self, conditions):
        result = []
        for record in self.fee_records:
            if all(record[key] == value for key, value in conditions.items()):
                result.append(record)
        return result

    # 查询支付记录
    def query_payment_records(self, conditions):
        result = []
        for record in self.payment_records:
            if all(record[key] == value for key, value in conditions.items()):
                result.append(record)
        return result

class FeeManagement:
    def __init__(self, db):
        self.db = db
# 费用录入
def record_fee(dorm_id, water_usage, electricity_usage, billing_period, water_rate, electricity_rate):
    # 计算水电费用
    water_fee = water_usage * water_rate
    electricity_fee = electricity_usage * electricity_rate
    total_fee = water_fee + electricity_fee
    # 检查费用是否符合预期
    if total_fee < 0:
        return "错误：费用计算不正确，请重新录入数据。"
    # 生成费用记录ID
    fee_id = str(uuid.uuid4())  # 生成唯一的费用记录ID
    # 生成费用记录数据
    fee_record = {
        "fee_id": fee_id,
        "dorm_id": dorm_id,
        "billing_period": billing_period,
        "water_usage": water_usage,
        "electricity_usage": electricity_usage,
        "water_fee": water_fee,
        "electricity_fee": electricity_fee,
        "total_fee": total_fee,
        "status": "未缴纳",
        "created_at": datetime.now()
    }
    # 将费用记录插入数据库
    insert_fee_record(fee_record)
    # 返回录入成功消息
    return "费用录入成功！"
# 模拟数据库插入操作
def insert_fee_record(fee_record):
    # 假设数据库插入操作
    print(f"录入数据到数据库: {fee_record}")


# 费用查询
def query_fees(dorm_id=None, student_id=None, student_name=None, start_date=None, end_date=None):
    # 构建查询条件
    query_conditions = {}
    if dorm_id:
        query_conditions['dorm_id'] = dorm_id
    if student_id:
        query_conditions['student_id'] = student_id
    if student_name:
        query_conditions['student_name'] = student_name
    if start_date and end_date:
        query_conditions['billing_period'] = {"$gte": start_date, "$lte": end_date}

    # 从数据库查询费用记录
    fee_records = query_fee_records(query_conditions)

    # 整理查询结果
    query_results = []
    for record in fee_records:
        query_results.append({
            "宿舍编号": record['dorm_id'],
            "水电用量": f"水: {record['water_usage']} L, 电: {record['electricity_usage']} kWh",
            "费用明细": f"水费: {record['water_fee']}元, 电费: {record['electricity_fee']}元",
            "缴费状态": record['status'],
            "缴费记录": f"总费用: {record['total_fee']}元"
        })
    # 返回查询结果
    return query_results
# 数据库查询
def query_fee_records(query_conditions):
    # 假设从数据库获取的记录
    fee_records = [
        {
            "dorm_id": "A101",
            "water_usage": 100, "electricity_usage": 200,
            "water_fee": 10, "electricity_fee": 30,
            "total_fee": 40, "status": "未缴纳", "billing_period": "2024年10月"
        }
    ]
    return fee_records
from datetime import datetime
# 费用缴纳
def pay_fee(fee_id, student_id, amount, payment_method):
    # 根据 fee_id 查找对应的费用记录
    fee_record = get_fee_record_by_id(fee_id)
    # 验证缴费金额是否正确
    total_fee = fee_record['total_fee']
    if amount != total_fee:
        return "错误：缴纳金额与应缴金额不匹配。"
    # 生成支付记录
    payment_id = str(uuid.uuid4())  # 生成唯一的支付记录ID
    payment_time = datetime.now()
    payment_status = "成功"  # 假设支付成功
    payment_record = {
        "payment_id": payment_id,
        "fee_id": fee_id,
        "student_id": student_id,
        "amount": amount,
        "payment_method": payment_method,
        "payment_time": payment_time,
        "payment_status": payment_status
    }
    # 更新费用记录状态为“已缴纳”
    update_fee_status(fee_id, "已缴纳")
    # 保存支付记录到数据库
    save_payment_record(payment_record)
    # 返回支付成功消息
    return "支付成功！"
# 从数据库获取费用记录
def get_fee_record_by_id(fee_id):
    # 假设根据 fee_id 获取记录
    return {
        "fee_id": fee_id,
        "total_fee": 40
    }
# 更新费用状态
def update_fee_status(fee_id, status):
    # 假设更新费用记录状态
    print(f"更新费用记录 {fee_id} 状态为：{status}")
# 保存支付记录到数据库
def save_payment_record(payment_record):
    # 假设数据库操作
    print(f"保存支付记录：{payment_record}")


import pandas as pd

# 报表生成
def generate_report(start_date=None, end_date=None, payment_method=None):
    # 构建查询条件
    query_conditions = {}
    if start_date and end_date:
        query_conditions['payment_time'] = {"$gte": start_date, "$lte": end_date}
    if payment_method:
        query_conditions['payment_method'] = payment_method

    # 从数据库查询支付记录
    payment_records = query_payment_records(query_conditions)

    # 整理查询结果并汇总
    report_data = []
    total_amount = 0
    for record in payment_records:
        total_amount += record['amount']
        report_data.append({
            "学生编号": record['student_id'],
            "费用记录ID": record['fee_id'],
            "支付金额": record['amount'],
            "支付方式": record['payment_method'],
            "支付时间": record['payment_time']
        })

    # 使用 pandas 生成报表
    df = pd.DataFrame(report_data)
    report_filename = "报表.xlsx"
    df.to_excel(report_filename, index=False)

    # 返回报表生成成功信息
    return f"报表生成成功，文件保存在 {report_filename}"


# 模拟数据库查询支付记录
def query_payment_records(query_conditions):
    # 假设从数据库获取的支付记录
    payment_records = [
        {
            "student_id": "12345", "fee_id": "a1b2c3", "amount": 40,
            "payment_method": "微信支付", "payment_time": "2024-10-15 12:00"
        }
    ]
    return payment_records
